<?php 
	session_start();
	include("ketnoi.php");
	include ("smtp.php");
?>
<style type="text/css">
<!--
.error, .notice, .success {padding:.8em;margin:auto; margin-bottom:.5em;border:2px solid #ddd;width:570px;text-align:left}
.error {background:#FBE3E4;color:#8a1f11;border-color:#FBC2C4;}
.notice {background:#FFF6BF;color:#514721;border-color:#FFD324;}
.success {background:#E6EFC2 url(src/accept.png) no-repeat scroll left 5px;color:#006600;border-color:#C6D880;font-family: Lucida Grande,Verdana,Geneva,Sans-serif;}
.error a {color:#8a1f11;}
.notice a {color:#514721;}
.success a {color:#264409;}
.style1dk {font-size: 22px; font-weight:bold; font-family: "Courier New", Courier, monospace}
.style6dk {font-family: "Courier New", Courier, monospace; font-weight: bold; color:#333333 }
#dk{
width:235px;
border:1px solid #FF00FF;padding-bottom:2px; padding-top:2px;}
.style1 {color: #990000}
-->
</style>
<SCRIPT language=JavaScript>
function Validate_String(string, return_invalid_chars) {
  valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  invalid_chars = '';
  if(string == null || string == '')
     return(true);

  //For every character on the string.   
  for(index = 0; index < string.length; index++) {
    char = string.substr(index, 1);                        
     
    //Is it a valid character?
    if(valid_chars.indexOf(char) == -1) {
      //If not, is it already on the list of invalid characters?
      if(invalid_chars.indexOf(char) == -1) {
        //If it's not, add it.
        if(invalid_chars == '')
          invalid_chars += char;
        else
          invalid_chars += ', ' + char;
      }
    }
  }
  if(return_invalid_chars == true && invalid_chars != '') {
    last_comma = invalid_chars.lastIndexOf(',');
    if(last_comma != -1)
      invalid_chars = invalid_chars.substr(0, $last_comma) + 
      ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
    return(invalid_chars);
  }
  else
    return(invalid_chars == ''); 
}

function kiemtra(){
	
			var email = document.Form.email.value;	
			var text = document.Form.text.value;		   
			            if(text.length < 30 ){
			               alert("Nội dung thông diệp phải lớn hơn 30 ký tự!");
					       return (false);
			            }
			            at = email.indexOf('@');
                        dot = email.indexOf('.');
                        if(at == -1 || dot == -1 || dot <= at + 1 ||dot == 0 || dot == email.length - 1)
						{
  	                       alert("Email chưa chính xác");
                           return(false);
                        }
     
                        user_name = email.substr(0, at);
                        domain_name = email.substr(at + 1, email.length);                  
                        if(Validate_String(user_name) === false || Validate_String(domain_name) === false)
						{
						    alert("Email chưa chính xác");
							return(false);
						}
 
}
</script>
<div class="main">

	<h5>HỎI ĐÁP<HR ></h5>

</div>
<?php	
	$query = "select * from `".$dulieu."`.`hoidap` where NoiDungTL != ''  order by MaCH desc";	
	$kq = mysql_query($query);
	$tong=mysql_num_rows($kq);
	$sotrang = ceil($tong/10);
	
	if(isset($_REQUEST['trang']))
		$trang = $_REQUEST['trang'];
	else 
		$trang = 1;
	$dem = -1;

if($tong>0)
{
?>
<div style="margin:0 10px;padding-top:0px; font-family:'Times New Roman', Times, serif">
 <?php
	while($row = mysql_fetch_array($kq))
	{
		$dem++;
		if($dem >= ($trang-1)*10 && $dem <= ($trang*10)-1)
		{ 
		?>
		<div class="news_content" style="font-weight:bold;padding-right:10px; padding-left:20px"> <?php echo $dem +1; ?>.
	<a class="detail" style=" font-weight:bold;color: #000; font-size:15px; text-decoration:none" href="hd01<?php echo $row["MaCH"]?>.html" title="<?php echo $row['TieuDe']?>" rel="nofollow"><?php echo catchuoi($row['TieuDe'],0,110)?> 	</a>
</div>
		<?php
		}
	}
		?>
</div>
<?php 
if($sotrang > 1){
?>
<div style="padding: 10px;">
<table class="page" width="100%"><tr>
			<td width="100%" align="left" class="trang">
			<?php if($trang >1){?> <a href="hoidap-p<?php echo $trang-1?>.html">« back</a>...<?php } 
			
					for($i=1; $i<=$sotrang; $i++)
					{ 
					echo " | ";
					if($trang == $i) echo '<b>';
					?>
					
					  <a  href="hoidap-p<?php echo $i?>.html"><?php echo $i ?> </a><?php if($trang == $i) echo '</b>'; 
					  
					}
					echo " | ";
					if($trang < $sotrang){?>...<a href="hoidap-p<?php echo $trang+1?>.html">next »</a> <?php }?>	</td>
  		</tr>
	</table>
</div>
<?php
}

}
?>
<?php
if(isset($_POST['name']))
{	

	$hoten=$_POST['name'];
	$sdt=$_POST['sdt'];
	$email=$_POST['email'];
	$tieude=$_POST['subject'];
	$noidung=$_POST['text'];
	
	$error="";
	$notice="";
	$success="";
	
	$hop_le = true; // ban đầu cho biến này là true
	
	if($_POST['name']=="" || $_POST['email']=="" ||  $_POST['sdt']=="" || $_POST['subject']=="" || $_POST['text']==""){
	  $notice="Vui lòng nhập đầy đủ các field (*).";
	  $hop_le = false;
    }
	
	if ($hop_le == true)
    {
        $query = "INSERT INTO `".$dulieu."`.`hoidap`
		(TenNH,TieuDe,Email,CauHoi,NgayHoi,SDT) 
		VALUES('$hoten','$tieude','$email','$noidung', now(), '$sdt')";
		mysql_query($query); 

    $query22 = "select * from  `".$dulieu."`.`trungtam` ";
				$kq22 = mysql_query($query22);
				$row22 = mysql_fetch_array($kq22);
    $name = $hoten;
    $from = $row22['mail'];
    $to = $email;
    $subject = "Hỏi Đáp";
    $message = "<b>".$tieude."</b><br>".$noidung."<br><br><br>Chào ".$name."!<br>Câu hỏi của bạn đã được ghi nhận <br> Cám ơn vì đã quan tâm đến ".$row22['web']." , chúng tôi sẽ trả lời bạn trong thời gian nhanh nhất.<br><br><br>".$row22['Tentv'];
    $header = "From: ".$from."\r\nReply-to: ".$from;
    SendMail($from,$to,$subject,$message,$row22['web']); 
	   $success="Câu hỏi của bạn đã được ghi nhận. Xin cám ơn! <br> Quản trị viên sẽ kiểm tra trước khi trả lời bạn.";
    }
	
}


  if($success!=""){
  ?>
  <div class="success" align="center"><img src='images/checkin.png' width='15px' height='15px'/> <?php echo $success ?></div>
  <script language="javascript" type="text/javascript">
    function reFresh() {
      window.location='hoidap-p1.html';
   }
    window.setInterval("reFresh()",3000);
  </script>
  <?php
  }
  if($notice!=""){
	?>
	 <div class="notice" align="center"><img src='images/cancel_f2.png' width='10px' height='10px'/> <?php echo $notice ?></div>
  <?php
  }
  ?>
  <p>&nbsp;</p><p>&nbsp;</p>
  <div style="height: 20px; text-align:center; font-weight:bold; color:#666666">HÃY VIẾT CÁC THẮC MẮC CỦA BẠN</div>
<form action="hoidap-p1.html" method="post" name="Form" id="emailForm" class="form-validate" onSubmit="return kiemtra()">
         <table class="comment" id="tblComment" align="center" style="width: 620px;border: 1px solid rgb(197, 194, 194);background: rgb(242, 241, 241);margin: 5px 80px;padding: 0;padding-bottom: 10px;">
			<tbody>
            			<tr>
				<td colspan="2">
					<div>
						<div>
						  <label class="red"><b>Các câu hỏi của các bạn sẻ giúp chúng tôi hoàn thiện hơn.</b></label></div>
          
                   </div>				</td>
			</tr>
            <tr>
				<td colspan="2" height="10" style="color:#F00">Những thông tin (*) là bắt buộc</td>
			</tr>
			<tr>
			  <td width="25%"><label>Họ tên&nbsp;&nbsp;&nbsp;&nbsp; : <font color="#CC3300">(*)</font></label></td>
				<td width="75%"><input name="name" id="contact_name" size="30" class="inputbox" type="text" value="<?php echo $_POST['name']?>"> </td>
			</tr>
			<tr>
				<td><label>Email&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <font color="#CC3300">(*)</font></label></td>
				<td><input id="contact_email" name="email" size="30" class="inputbox required validate-email" maxlength="100" type="text" value="<?php echo $_POST['email']?>"></td>
			</tr>
            			<tr>
				<td><label>Số điện thoại&nbsp;: <font color="#CC3300">(*)</font></label></td>
				<td><input id="contact_email" name="sdt" size="30" class="inputbox required validate-email" maxlength="100" type="text" value="<?php echo $_POST['sdt']?>"></td>
			</tr>
            			<tr>
				<td><label>Tiêu đề&nbsp;&nbsp;&nbsp;&nbsp;: <font color="#CC3300">(*)</font></label></td>
				<td><input id="contact_email" name="subject" size="30" class="inputbox required validate-email" maxlength="100" type="text" value="<?php echo $_POST['subject']?>"></td>
			</tr>
			<tr>
				<td valign="top" style="padding-top:3px;"><label>Nội dung : <font color="#CC3300">(*)</font></label></td>
				<td>
					<textarea cols="40" rows="10" name="text" id="contact_text" class="inputbox required"><?php echo $_POST['text']?></textarea>			</td>
			</tr>

			
			<tr>
				<td colspan="2">
					<div align="center">
                    				
				       
						<input name="submit" class="button_1" value=" Gửi " type="submit">
                         <input type="reset" class="button_1"  name="reset" value="Hủy" />
					</div>				</td>
			</tr>
		</tbody></table>	
  </form>
  